package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "SPU基础属性接口")
@RestController
@RequestMapping("admin/product")
public class SpuManagerController {
    @Autowired
    private ManageService manageService;

    //GET/admin/product/{page}/{limit}
    @ApiOperation("获取SPU属性 回显到页面")
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit,
//                从服务端传入的条件  解决方法
//                 方法一         @RequestParam Long category3Id,
//                 方法二          Long category3Id,
//                 方法三 1         HttpServletRequest request,
//                 方法四 SpingMVC对象传值
                                 SpuInfo spuInfo){
//   方法三 2  String category3Id = request.getParameter("category3Id");
        //创建分页类
        Page iPage = new Page(page,limit);
        IPage supInfoPage = manageService.getSpuInfoPage(iPage,spuInfo);
        return Result.ok(supInfoPage);
    }

    //商品管理 获取销售属性数据
    //GET/admin/product/baseSaleAttrList
    @ApiOperation("商品管理 获取销售属性数据")
    @GetMapping("baseSaleAttrList")
    public Result getBaseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = manageService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //商品管理 保存spu
    //POST/admin/product/saveSpuInfo
    @ApiOperation("保存spu")
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){

        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }
    //Request URL: /admin/product/spuImageList/{spuId}
    //Request Method: GET 商品信息管理 根据spuId 获取spuImage 集合
    @ApiOperation("根据spuId 获取spuImage 集合")
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){

        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }
    //Request URL: /admin/product/spuSaleAttrList/{spuId}
    //Request Method: GET  根据spuId 查询销售属性
    @ApiOperation("根据spuId 查询销售属性")
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

}
